package com.yaozheng.vocationaltraining.view;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.LearnListTypeAdapter;
import com.yaozheng.vocationaltraining.adapter.event.ItemClickListener;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.sf.json.JSONObject;

/**
 * 学习列表类型一项view
 *
 * @author Administrator
 */
@EViewGroup(R.layout.item_learn_list_type)
public class LearnListTypeItemView extends LinearLayout {
    @ViewById
    View learnListTypeItemLayout;
    @ViewById
    TextView learnListTypeItemText;
    @ViewById
    ImageView learnListTypeItemImageView;
    private JSONObject itemData;
    int position;

    ItemClickListener<LearnListTypeItemView> itemClickListener;
    LearnListTypeAdapter learnListTypeAdapter;
    @ViewById
    View learnListTypeItemBorder;

    public void setLearnListTypeAdapter(
            LearnListTypeAdapter learnListTypeAdapter) {
        this.learnListTypeAdapter = learnListTypeAdapter;
    }


    public LearnListTypeItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public LearnListTypeItemView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void setItemClickListener(ItemClickListener<LearnListTypeItemView> itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void loadData(JSONObject itemData, int position, BitmapLoader bitmapLoader) {
        this.position = position;
        this.itemData = itemData;
        learnListTypeItemText.setText(TypeUtils.getJsonString(itemData, "name"));
//        learnListTypeItemText.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(TypeUtils.getJsonInteger(itemData, "imageResourcesId", R.drawable.icon_study_class1)), null, null, null);
        bitmapLoader.displayImage(TypeUtils.getJsonString(itemData, "cate_icon"), learnListTypeItemImageView);
        if (learnListTypeAdapter != null
                && position == learnListTypeAdapter.getSelectPosition()) {
            learnListTypeItemLayout.setSelected(true);

        } else {
            learnListTypeItemLayout.setSelected(false);
        }
        learnListTypeItemText.setEnabled(TypeUtils.getJsonBoolean(itemData, "isEnable", true));

    }


    public void refreshSelect(){
        if (position == TypeUtils.getJsonArraySize(learnListTypeAdapter.getDataList()) - 1) {
            learnListTypeItemBorder.setVisibility(View.INVISIBLE);
        } else {
            learnListTypeItemBorder.setVisibility(View.VISIBLE);
        }
    }

    public JSONObject getItemData() {
        return itemData;
    }

    public int getPosition() {
        return position;
    }

    @Click(R.id.learnListTypeItemLayout)
    public void itemClick() {
//        boolean isEnable = TypeUtils.getJsonBoolean(itemData, "isEnable", true);
//        if (isEnable) {
//            if (learnListTypeAdapter != null) {
//                learnListTypeAdapter.setSelectPosition(position);
//                learnListTypeAdapter.notifyDataSetChanged();
//            }
//        }
        if (itemClickListener != null) {
//            learnListTypeAdapter.setSelectPosition(position);
            itemClickListener.click(this);
        }
    }

}
